package com.wsj.web.action.admin.file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ImagesOperAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		String temp_path_url = request.getParameter("desc");
		Object temp_path_bea = request.getSession().getAttribute("pathSelected");
		String path = "";
	        if(temp_path_url!=null && !"".equals(temp_path_url)){
	        	 path = temp_path_url;
	        	 request.getSession().setAttribute("pathSelected", path);
	        } else if(null!=temp_path_bea && !"".equals(temp_path_bea.toString())){
	        	 path = temp_path_bea.toString();
	        	 request.getSession().setAttribute("pathSelected", path);
	        }else{
	        	 path = "recommend";
	        	 request.getSession().setAttribute("pathSelected", path);
	        }   	    
		List<String> imgnamelist = new ArrayList<String>();
		String[] nlst = this.getFileNameList(path);
		List plst = this.getFilePathList();
		if(null!=nlst && nlst.length!=0){
			for(int i=nlst.length-1;i>=0;i--){
				imgnamelist.add(nlst[i]);
			}
		}
		request.setAttribute("plst", plst);
		request.setAttribute("imglist", imgnamelist);
		return mapping.findForward("success");
	}
	
	
   private String[] getFileNameList(String path){
		String[] namelist=null;
		try{   
			 String filePath =   "";     
		     filePath = this.getServlet().getServletContext().getRealPath(File.separator)+"fileUpload"+File.separator+path+File.separator;	
		     File file = new File(filePath);
			     if(file.exists()){
				     Filter filter = new Filter(".jpg");
				     namelist = file.list(filter); 
			     }
		}catch(Exception e){ 
			   e.printStackTrace();
		}  
		 return namelist;
	 }
   
   private List getFilePathList(){
	   List<String> pathlist = new ArrayList<String>();
	   try {
		   String filePath =   "";     
		     filePath = this.getServlet().getServletContext().getRealPath(File.separator)+"fileUpload"+File.separator;	
		     File path = new File(filePath);
		       if(path.exists() && path.isDirectory()){
		    	    File[] listFile = path.listFiles();
		    	    if(listFile!=null){
		    	    	for(int i=0;i<listFile.length;i++){
		    	    		if(listFile[i].isDirectory()){
		    	    			String ss = listFile[i].getPath();
		    	    		    String s2 = ss.substring(ss.lastIndexOf(File.separator)+1,ss.length());
		    	    			pathlist.add(s2);
		    	    		}	
		    	    	}
		    	    }
		       }else{
		    	   pathlist.add("recommend");
		       }
		   
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}
	return pathlist;
   }
   
   class Filter implements FilenameFilter{
        String extension;
        Filter(String extension){
             this.extension=extension;
        }
        public boolean accept(File directory,String filename){
           return (filename.endsWith(".jpg") || filename.endsWith(".gif"));
        }
   }

}
	

